/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package researchgrants.parts.GrantRequest.agency;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import researchgrants.parts.db.Db;

/**
 * A list of agencies
 * Maintain the list of agencies
 * support getAllAgenciesList to retreive the list of all agencies
 * @author DOStudent1
 */
public class AgenciesList implements List<Agency> {

    public <T> T[] toArray(T[] a) {
        return list.toArray(a);
    }

    public Object[] toArray() {
        return list.toArray();
    }

    public List<Agency> subList(int fromIndex, int toIndex) {
        return list.subList(fromIndex, toIndex);
    }

    public int size() {
        return list.size();
    }

    public Agency set(int index, Agency element) {
        return list.set(index, element);
    }

    public boolean retainAll(Collection<?> c) {
        return list.retainAll(c);
    }

    public boolean removeAll(Collection<?> c) {
        return list.removeAll(c);
    }

    public Agency remove(int index) {
        return list.remove(index);
    }

    public boolean remove(Object o) {
        return list.remove(o);
    }

    public ListIterator<Agency> listIterator(int index) {
        return list.listIterator(index);
    }

    public ListIterator<Agency> listIterator() {
        return list.listIterator();
    }

    public int lastIndexOf(Object o) {
        return list.lastIndexOf(o);
    }

    public Iterator<Agency> iterator() {
        return list.iterator();
    }

    public boolean isEmpty() {
        return list.isEmpty();
    }

    public int indexOf(Object o) {
        return list.indexOf(o);
    }

    public int hashCode() {
        return list.hashCode();
    }

    public Agency get(int index) {
        return list.get(index);
    }

    public boolean equals(Object o) {
        return list.equals(o);
    }

    public boolean containsAll(Collection<?> c) {
        return list.containsAll(c);
    }

    public boolean contains(Object o) {
        return list.contains(o);
    }

    public void clear() {
        list.clear();
    }

    public boolean addAll(int index, Collection<? extends Agency> c) {
        return list.addAll(index, c);
    }

    public boolean addAll(Collection<? extends Agency> c) {
        return list.addAll(c);
    }

    public void add(int index, Agency element) {
        list.add(index, element);
    }

    public boolean add(Agency e) {
        return list.add(e);
    }

    private List<Agency> list=new LinkedList<Agency> ();
   
    // Returns a list with all agencies.
    public static AgenciesList getAllAgenciesList() {
            AgenciesList agencieslist = null;
        try {
            agencieslist = new AgenciesList();
            Connection conn = Db.openDbConnection();
            PreparedStatement prepareStatement = Db.createPreparedStatement(conn, "SELECT AgencyID FROM tblAgencies");
            ResultSet agencyResultSet = prepareStatement.executeQuery();
            Db.moveToFirstLine(agencyResultSet);

            agencieslist.add(Agency.getById((Integer) Db.getColumn(agencyResultSet, 1)));

            agencyResultSet.close();
            prepareStatement.close();
            Db.closeDbConnection(conn);
        } catch (SQLException ex) {
            Logger.getLogger(AgenciesList.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(AgenciesList.class.getName()).log(Level.SEVERE, null, ex);
        }
        return agencieslist;
    }
    
}
